package com.xbj.cloud.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.xbj.cloud.db.pojo.Pay;
import com.xbj.cloud.db.pojo.PayDTO;
import com.xbj.cloud.commons.resp.ResultData;
import com.xbj.cloud.service.PayService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: 观雾
 * @create: 2024-07-16
 * @description:
 **/
@RestController
@Tag(name = "支付微服务模块",description = "支付CRUD")
public class PayController {
    private static final Logger LOG = LoggerFactory.getLogger(PayController.class);
    @Resource
    private PayService payService;

    @PostMapping(value = "/pay/add1")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public String addPay1(@RequestBody Pay pay){
        int addCount = payService.add(pay);

        return "成功插入"+addCount+"记录";
    }
    @PostMapping(value = "/pay/add2")
    @Operation(summary = "新增",description = "新增支付流水方法,json串做参数")
    public ResultData<String> addPay2(@RequestBody Pay pay){
        int addCount = payService.add(pay);

        ResultData<String> success = ResultData.success("成功插入"+addCount+"记录");
        return success;
    }

    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除",description = "删除支付流水方法")
    public Integer detelePay(@PathVariable("id") Integer id){
        int deleteCount = payService.delete(id);
        return deleteCount;
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改",description = "修改支付流水方法")
    public String updatePay(@RequestBody PayDTO payDTO){
        Pay pay = new Pay();
        BeanUtil.copyProperties(payDTO,pay);
        int updateCount = payService.update(pay);
        return "成功更新"+updateCount+"记录";
    }

    @GetMapping(value = "/pay/getById/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法")
    public ResultData getById(@PathVariable("id") Integer id){
        Pay pay = payService.getById(id);
        ResultData<Pay> success = ResultData.success(pay);
        return success;
    }
    @GetMapping(value = "/pay/getByIdTimeOut/{id}")
    @Operation(summary = "按照ID查流水",description = "查询支付流水方法-超时")
    public ResultData getByIdTimeOut(@PathVariable("id") Integer id){
        Pay pay = payService.getById(id);
        ResultData<Pay> success = ResultData.success(pay);
        try {
            TimeUnit.SECONDS.sleep(6);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return success;
    }

    @GetMapping(value = "/pay/getById")
    public Pay getById(){
        Pay pay = payService.getById(2);
        LOG.info("pay"+ JSONUtil.toJsonStr(pay));
        return pay;
    }
    @GetMapping(value = "/pay/getAll")
    public List<Pay> getAll(){
        List<Pay> payList = payService.getAll();
        return payList;
    }

    @GetMapping(value = "/pay/getError")
    @Operation(summary = "测试全局异常处理",description = "测试全局异常处理")
    public void getError(){
       // int i = 1/0;
        throw new NullPointerException();
//         try {
//             int i = 1/0;
//         }catch (Exception e){
//             LOG.error(e.getMessage()+"======="+e);
//         }
//        throw new RuntimeException("测试全局异常处理");
    }

    @Value("${server.port}")
    private String port;

    @GetMapping(value = "/pay/getConsulConfigTest")
    public String getConsulConfigTest(@Value("${xCloud.info}") String xcloudInfo){
        LOG.info("xCloud.info:"+xcloudInfo +"\t" +"port:"+port);
        return "xCloud.info:"+xcloudInfo +"\t" +"port:"+port;
    }


}
